package com.crm.web.controller.business;

import java.util.Arrays;
import java.util.List;

import com.crm.common.core.domain.entity.SysUser;
import com.crm.common.utils.ShiroUtils;
import com.crm.system.domain.BiePatient;
import com.crm.system.service.IBiePatientService;
import com.crm.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.crm.common.annotation.Log;
import com.crm.common.enums.BusinessType;
import com.crm.system.domain.BieInquiry;
import com.crm.system.service.IBieInquiryService;
import com.crm.common.core.controller.BaseController;
import com.crm.common.core.domain.AjaxResult;
import com.crm.common.utils.poi.ExcelUtil;
import com.crm.common.core.page.TableDataInfo;

/**
 * 问诊记录Controller
 * 
 * @author crm
 * @date 2021-05-10
 */
@Controller
@RequestMapping("/system/inquiry")
public class BieInquiryController extends BaseController
{
    private String prefix = "system/inquiry";

    @Autowired
    private IBieInquiryService bieInquiryService;

    @Autowired
    private ISysUserService userService;


    @Autowired
    private IBiePatientService biePatientService;



    @RequiresPermissions("system:inquiry:view")
    @GetMapping()
    public String inquiry()
    {
        return prefix + "/inquiry";
    }

    /**
     * 查询问诊记录列表
     */
    @RequiresPermissions("system:inquiry:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BieInquiry bieInquiry)
    {
        startPage();
       // Long[] roleIds = ShiroUtils.getSysUser().getRoleIds();
        //判断当前用户是否为管理员
//        if(!ShiroUtils.getSysUser().isAdmin()){
//            //如果不是管理员，则只显示自己的
//            Long userId = ShiroUtils.getUserId();
//            BiePatient biePatient = new BiePatient();
//            biePatient.setUserId(userId);
//            System.out.println(userId);
//            List<BiePatient> biePatients = biePatientService.selectBiePatientList(biePatient);
//            String patientId = biePatients.get(0).getPatientId();
//            bieInquiry.setPatientCode(patientId);
//        }


        //只有患者显示自己的问诊
        Long[] roleIds = ShiroUtils.getSysUser().getRoleIds();
        //Arrays.stream(roleIds).isParallel()
        //患者
        SysUser sysUser = ShiroUtils.getSysUser();
       // System.out.println(sysUser);

        //System.out.println(roleIds);

      //  System.out.println(sysUser.getRoleId());
        if(sysUser.getRoles().get(0).getRoleId().equals(4L)){
            Long userId = ShiroUtils.getUserId();
            BiePatient biePatient = new BiePatient();
            biePatient.setUserId(userId);
            System.out.println(userId);
            List<BiePatient> biePatients = biePatientService.selectBiePatientList(biePatient);
            String patientId = biePatients.get(0).getPatientId();
            bieInquiry.setPatientCode(patientId);
        }

//        for (Long roleId : roleIds) {
//            if(roleId.equals(4L)){
//                //只显示自己的
//                Long userId = ShiroUtils.getUserId();
//                BiePatient biePatient = new BiePatient();
//                biePatient.setUserId(userId);
//                System.out.println(userId);
//                List<BiePatient> biePatients = biePatientService.selectBiePatientList(biePatient);
//                String patientId = biePatients.get(0).getPatientId();
//                bieInquiry.setPatientCode(patientId);
//            }
//
//                break;
//        }

        List<BieInquiry> list = bieInquiryService.selectBieInquiryList(bieInquiry);
        return getDataTable(list);
    }

    /**
     * 导出问诊记录列表
     */
    @RequiresPermissions("system:inquiry:export")
    @Log(title = "问诊记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BieInquiry bieInquiry)
    {
        List<BieInquiry> list = bieInquiryService.selectBieInquiryList(bieInquiry);
        ExcelUtil<BieInquiry> util = new ExcelUtil<BieInquiry>(BieInquiry.class);
        return util.exportExcel(list, "问诊记录数据");
    }

    /**
     * 新增问诊记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存问诊记录
     */
    @RequiresPermissions("system:inquiry:add")
    @Log(title = "问诊记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BieInquiry bieInquiry)
    {
        String loginName = ShiroUtils.getLoginName();
        //SysUser user = userService.selectUserByLoginName(loginName);

        Long userId = ShiroUtils.getUserId();
        BiePatient biePatient = new BiePatient();
        biePatient.setUserId(userId);
        System.out.println(userId);
        List<BiePatient> biePatients = biePatientService.selectBiePatientList(biePatient);
        String patientId = biePatients.get(0).getPatientId();
        bieInquiry.setPatientCode(patientId);




        System.out.println(bieInquiry);
        return toAjax(bieInquiryService.insertBieInquiry(bieInquiry));
    }

    /**
     * 修改问诊记录
     */
    @GetMapping("/edit/{inquiryId}")
    public String edit(@PathVariable("inquiryId") String inquiryId, ModelMap mmap)
    {
        BieInquiry bieInquiry = bieInquiryService.selectBieInquiryById(inquiryId);
        mmap.put("bieInquiry", bieInquiry);
        return prefix + "/edit";
    }

    /**
     * 修改保存问诊记录
     */
    @RequiresPermissions("system:inquiry:edit")
    @Log(title = "问诊记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BieInquiry bieInquiry)
    {
        return toAjax(bieInquiryService.updateBieInquiry(bieInquiry));
    }

    /**
     * 删除问诊记录
     */
    @RequiresPermissions("system:inquiry:remove")
    @Log(title = "问诊记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bieInquiryService.deleteBieInquiryByIds(ids));
    }
}
